/**
 * 
 */
package org.hackystat.ui.orb.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author David J. Nickles
 *
 */
public class CompositeWidget extends Composite implements EntryPoint {

  /**
   * This method builds the UI when the widget is loaded.
   */
  public void initLayout() {
    VerticalPanel vp = new VerticalPanel();
    vp.add(new SettingsPanel());
    initWidget(vp);
    RootPanel.get("widget").add(this);
  }
  
  /**
   * This method displays the passed version in the UI.
   * @param manifest an array of strings containing attributes from the manifest file.
   *  
   * @gwt.typeArgs manifest <java.lang.String>
   */
  public void digestManifest(ArrayList manifest) {
    Window.setTitle((String)manifest.get(0));
    RootPanel.get("version").add(new HTML("<b>v." + (String)manifest.get(1) + "</b>"));
    RootPanel title = RootPanel.get("text");
    if (title != null) {
      title.add(new HTML("<div style=\"color: white; font-weight: strong;\">" 
        + (String)manifest.get(0) + "</div>"));
    }
  }
  
  /**
   * Check with the servlet to see if the host is valid.
   */
  private void rpcReadManifest() {
    RPCServiceFactory.getInstance().getService().getManifest(new AsyncCallback() {
      public void onSuccess(Object result) {
        digestManifest((ArrayList) result);
      }
  
      public void onFailure(Throwable caught) {
        if (caught instanceof com.google.gwt.user.client.rpc.InvocationException) {
          log(RPCServiceFactory.PRCERRORNAME, "Cannot invoke version request on servlet.");
        } 
        else {
          log(RPCServiceFactory.PRCERRORNAME, caught.toString());
        }
      }
    });
  }

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    rpcReadManifest();
    initLayout();
  }
  
  /**
   * Log this message in the reader display.
   * @param header the message header or subject
   * @param body the message body
   */
  public void log(String header, String body) {
    Window.alert(header + " : " + body);
  }
}
